import http from './httpFactory2'
import qs from 'qs'
import {success, error} from "@/utils/msg"

export const axios = () => {
    return http;
}

export const getBaseURL = () => {
    return http.defaults.baseURL;
}
/**
 *
 * @param url
 * @param params
 * @param returnDataOnly boolean类型  只返回数据
 * @returns {Promise<unknown>}
 */
export const get = (url, params, returnDataOnly, isParams) => {
    params = params ? JSON.parse(JSON.stringify(params)) : {};
    return new Promise((resolve, reject) => {
        http.get(url, isParams == true ? {params: params} : {params}).then(res => {
            if (returnDataOnly != false) {
                if (res.code && res.code !== 200 && res.code !== 0) {
                    error(res.msg)
                    reject(res);
                }
                resolve(res.data)
            } else {
                resolve(res)
            }
        }).catch(err => {
            reject(err);
        })
    })
}
export const post = (url, params, autoMessage, returnStatus) => {
    return new Promise((resolve, reject) => {
        http.post(url, params).then(res => {
            message(res, autoMessage, returnStatus, resolve);
        }).catch(err => {
            reject(err);
        })
    })
}
export const request = (config, autoMessage, returnStatus) => {
    return new Promise((resolve, reject) => {
        http.request(config).then(res => {
            message(res, autoMessage, returnStatus, resolve);
        }).catch(err => {
            reject(err);
        })
    })
}
export const postQs = (url, params, autoMessage, returnStatus) => {

    return new Promise((resolve, reject) => {
        http.post(url, qs.stringify(params), {Accept: "KV"}).then(res => {
            message(res, autoMessage, returnStatus, resolve);
        }).catch(err => {
            reject(err);
        })
    })
}

export const put = (url, params, autoMessage, returnStatus) => {
    return new Promise((resolve, reject) => {
        http.put(url, params).then(res => {
            message(res, autoMessage, returnStatus, resolve);
        }).catch(err => {
            reject(err);
        })
    })
}

export const del = (url, params, autoMessage, returnStatus) => {
    return new Promise((resolve, reject) => {
        http({
            url: url, method: 'delete', params: params
        }).then(res => {
            message(res, autoMessage, returnStatus, resolve);
        }).catch(err => {
            reject(err);
        })
    })
}
export const upload = (url, params, onUploadProgress) => {
    return new Promise((resolve, reject) => {
        http.post(url, params,
            {
                timeout: 1000 * 90 * 90,
                onUploadProgress: onUploadProgress,
                headers: {'Content-Type': 'multipart/form-data'}
            }).then(res => {
            resolve(res)
        }).catch(err => {
            reject(err);
        })
    })
}

function message(res, autoMessage, returnStatus, resolve) {
    if (autoMessage || autoMessage == undefined) {
        if (res.code == 200 || res.code == 0) {
            if (res.msg) {
                success(res.msg)
            } else {
                success("操作成功！")
            }
        } else {
            error(res.msg)
        }
    }
    if (returnStatus || returnStatus == undefined) {
        resolve(res.code === 200 || res.code === 0)
    } else {
        resolve(res)
    }
}

export default {
    get, post, put, del, postQs, request, axios, getBaseURL, upload
}
